Resource
Wrapper TutorialInteractions with MLDB occurs via a REST API. Interacting with a REST API over HTTP from a Notebook interface can be a little bit laborious if you're using a general-purpose Python library like requests
directly, so MLDB comes with a Python library called pymldb
to ease the pain.
pymldb
does this in three ways:
Resource
class: this is simple class which wraps the requests
library so as to make HTTP calls to the MLDB API more friendly in a Notebook environment. This tutorial shows you how to use it.%mldb
magics: these are Jupyter line- and cell-magic commands which allow you to make raw HTTP calls to MLDB, and also provides some higher-level functions. Check out the Cell magic Tutorial for more info on the %mldb
magic system.BatFrame
class: this is a class that behaves like the Pandas DataFrame but offloads computation to the server via HTTP calls. Check out the BatFrame Tutorial for more info on the BatFrame.
In [16]:
from pymldb.resource import Resource
r = Resource("http://localhost")
print r
You can use a Resource
object to quickly create new Resource
objects to refer to different URLs by calling functions or passing in arguments, chaining the calls:
In [17]:
print type(r), r
x = r.x
print type(x), x
y = x("y")
print type(y), y
z = r("and").so("on")("and").so.on
print type(z), z
In [18]:
dataset_types = r.v1.types.datasets
dataset_types.get()
Out[18]:
The HTTP request is performed via the Python requests
library: arguments to get()
, post()
, put()
and delete()
are just delegated to the corresponding requests
function. The only thing that Resource
does to the result is patch it so it will display prettily in a Notebook, as above.
In [19]:
#keyword arguments to get_query() are appended to the GET query string
r.v1.types.get_query(x="y")
Out[19]:
In [20]:
sample_dataset = r.v1.datasets("sample")
sample_dataset.delete()
#dictionaries arguments to put_json() and post_json() are sent as JSON via PUT or POST
sample_dataset.put_json( {"type": "beh.mutable"} )
Out[20]:
Now that you've seen the basics, check out the Predicting Titanic Survival demo to see how to use the Resource
class to do machine learning with MLDB.
In [ ]: